﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class WebCustomUserControls_AutoGeneIdentifyNo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string o_id = this.Page.Request.Params["o_id"];//获得单据编号
            int p_type = Int32.Parse(this.Page.Request.Params["p_type"]);//获得单据类型

            this.o_id.Value = o_id;
            this.p_type.Value = p_type.ToString();

            this.ReBind(o_id, p_type);
        }
    }

    //修改贴现或者兑现
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        ClsSeekDAL cls_SeekDAL = new ClsSeekDAL();

        string p_id = this.p_id.Text.Trim();
        DateTime p_date = DateTime.Parse(this.p_date.Text);
        string p_bank = this.p_bank.Text.Trim();
        decimal p_lv = (this.p_lv.Text.Trim() == "")? 0 :(decimal.Parse(this.p_lv.Text.Trim()));
        decimal p_value = decimal.Parse(this.p_value.Text.Trim());
        decimal p_fy = decimal.Parse(this.p_fy.Text.Trim());
        decimal p_money = decimal.Parse(this.txje.Text.Trim());
        string bsUnit = this.bsUnit.Text.Trim();
        decimal bsje = decimal.Parse(this.bsje.Text.Trim());
        decimal lx = decimal.Parse(this.lx.Text.Trim());
        string oper = this.oper.Text.Trim();
        bool is_c_lx = (this.is_c_lx.SelectedIndex == 0) ? true : false;
        bool is_c_fy = (this.is_c_fy.SelectedIndex == 0) ? true : false;
        

        Boolean bolIsSuccess = cls_SeekDAL.UpdateSeek(o_id.Value,Int32.Parse(p_type.Value), p_id, p_date, p_bank, p_lv, p_value, p_fy, p_money, bsUnit, bsje, lx, is_c_lx, is_c_fy, oper);
        if (bolIsSuccess)
        {
            this.lblJs.Text = "<script>alert('修改成功！')</script>";
        }
        else
        {
            this.lblJs.Text = "<script>alert('修改失败！')</script>";
        }
        this.ReBind(o_id.Value, Int32.Parse(p_type.Value));
    }

    //删除贴现或者兑现
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        ClsSeekDAL cls_SeekDAL = new ClsSeekDAL();

        Boolean bolIsSuccess = cls_SeekDAL.DeleteSeek(Int32.Parse(p_type.Value), o_id.Value);
        if (bolIsSuccess)
        {
            this.lblJs.Text = "<script>alert('成功删除！')</script>";
        }
        else
        {
            this.lblJs.Text = "<script>alert('删除失败！')</script>";
        }
        this.ReBind(o_id.Value, Int32.Parse(p_type.Value));
    }

    //增加贴现或者兑现
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ClsSeekDAL cls_SeekDAL = new ClsSeekDAL();

        string p_id = this.p_id.Text.Trim();
        DateTime p_date = DateTime.Parse(this.p_date.Text);
        string p_bank = this.p_bank.Text.Trim();
        decimal p_lv = (this.p_lv.Text.Trim() == "") ? 0 : (decimal.Parse(this.p_lv.Text.Trim()));
        decimal p_value = decimal.Parse(this.p_value.Text.Trim());
        decimal p_fy = decimal.Parse(this.p_fy.Text.Trim());
        decimal p_money = decimal.Parse(this.txje.Text.Trim());
        string bsUnit = this.bsUnit.Text.Trim();
        decimal bsje = decimal.Parse(this.bsje.Text.Trim());
        decimal lx = decimal.Parse(this.lx.Text.Trim());
        string oper = this.oper.Text.Trim();
        bool is_c_lx = (this.is_c_lx.SelectedIndex == 0) ? true : false;
        bool is_c_fy = (this.is_c_fy.SelectedIndex == 0) ? true : false;


        Boolean bolIsSuccess = cls_SeekDAL.AddSeek(o_id.Value,Int32.Parse(p_type.Value), p_id, p_date, p_bank, p_lv, p_value, p_fy, p_money, bsUnit, bsje, lx, is_c_lx, is_c_fy, oper);
        if (bolIsSuccess)
        {
            this.lblJs.Text = "<script>alert('成功保存！')</script>";
        }
        else
        {
            this.lblJs.Text = "<script>alert('添加失败！')</script>";
        }
        this.ReBind(o_id.Value, Int32.Parse(p_type.Value));
    }

    public void ReBind(string o_id,int p_type)
    {
        ClsSeekDAL cls_SeekDAL = new ClsSeekDAL();
        DataSet dst = new DataSet();

        dst = cls_SeekDAL.GetSeek(o_id, p_type);

            if (p_type == 0)//贴现
            {
                if (dst != null && dst.Tables[0].Rows.Count > 0)
                {
                    //数据
                    this.p_id.Text = dst.Tables[0].Rows[0]["p_id"].ToString();
                    this.p_date.Text = dst.Tables[0].Rows[0]["p_date"].ToString();
                    this.p_bank.Text = dst.Tables[0].Rows[0]["p_bank"].ToString();
                    this.p_lv.Text = dst.Tables[0].Rows[0]["p_lv"].ToString();
                    this.p_value.Text = dst.Tables[0].Rows[0]["p_value"].ToString();
                    this.p_fy.Text = dst.Tables[0].Rows[0]["p_fy"].ToString();
                    this.txje.Text = dst.Tables[0].Rows[0]["p_money"].ToString();
                    this.bsUnit.Text = dst.Tables[0].Rows[0]["bsUnit"].ToString();
                    this.bsje.Text = dst.Tables[0].Rows[0]["bsje"].ToString();
                    this.lx.Text = dst.Tables[0].Rows[0]["lx"].ToString();
                    this.oper.Text = dst.Tables[0].Rows[0]["oper"].ToString();


                    this.is_c_lx.SelectedIndex = (dst.Tables[0].Rows[0]["is_c_lx"].ToString() == "True") ? 0 : 1;
                    this.is_c_fy.SelectedIndex = (dst.Tables[0].Rows[0]["is_c_fy"].ToString() == "True") ? 0 : 1;

                    this.is_c_lx.DataBind();
                    this.is_c_fy.DataBind();

                    this.btnAdd.Enabled = false;
                    this.btnDelete.Enabled = true;
                    this.btnUpdate.Enabled = true;
                }
                else
                {
                    this.p_id.Text = "";
                    this.p_date.Text = "";
                    this.p_bank.Text = "";
                    this.p_lv.Text = "";
                    this.p_value.Text = "";
                    this.p_fy.Text = "";
                    this.txje.Text = "";
                    this.bsUnit.Text = "";
                    this.bsje.Text = "";
                    this.lx.Text = "";
                    this.oper.Text = "";
                    this.is_c_lx.Text = "";
                    this.is_c_fy.Text = "";
                }
                //标题
                this.Label1.Text = "贴现单号";
                this.Label2.Text = "贴现日期";
                this.Label3.Text = "贴现银行";
                this.Label4.Text = "贴现率";
                this.Label5.Text = "票据面值";
                this.Label6.Text = "费用";
                this.Label7.Text = "贴现净额";
                this.Label8.Text = "背书单位";
                this.Label9.Text = "背书金额";
                this.Label10.Text = "利息";
                this.Label11.Text = "录单人";
                this.Label12.Text = "是否公司承担利息";
                this.Label13.Text = "是否公司承担费用";
            }
            else//兑现
            {
                if (dst != null && dst.Tables[0].Rows.Count > 0)
                {
                    //数据
                    this.p_id.Text = dst.Tables[0].Rows[0]["p_id"].ToString();
                    this.p_date.Text = dst.Tables[0].Rows[0]["p_date"].ToString();
                    this.p_bank.Text = "";
                    this.p_lv.Text = "";
                    this.p_value.Text = dst.Tables[0].Rows[0]["p_value"].ToString();
                    this.p_fy.Text = dst.Tables[0].Rows[0]["p_fy"].ToString();
                    this.txje.Text = dst.Tables[0].Rows[0]["p_money"].ToString();
                    this.bsUnit.Text = dst.Tables[0].Rows[0]["bsUnit"].ToString();
                    this.bsje.Text = dst.Tables[0].Rows[0]["bsje"].ToString();
                    this.lx.Text = dst.Tables[0].Rows[0]["lx"].ToString();
                    this.oper.Text = dst.Tables[0].Rows[0]["oper"].ToString();
                    this.is_c_lx.Text = dst.Tables[0].Rows[0]["is_c_lx"].ToString();
                    this.is_c_fy.Text = dst.Tables[0].Rows[0]["is_c_fy"].ToString();

                    this.btnAdd.Enabled = false;
                    this.btnDelete.Enabled = true;
                    this.btnUpdate.Enabled = true;
                }
                else
                {
                    this.p_id.Text = "";
                    this.p_date.Text = "";
                    this.p_bank.Text = "";
                    this.p_lv.Text = "";
                    this.p_value.Text = "";
                    this.p_fy.Text = "";
                    this.txje.Text = "";
                    this.bsUnit.Text = "";
                    this.bsje.Text = "";
                    this.lx.Text = "";
                    this.oper.Text = "";
                    this.is_c_lx.Text = "";
                    this.is_c_fy.Text = "";
                }

                //标题
                this.Label1.Text = "兑现单号";
                this.Label2.Text = "兑现日期";
                this.Label3.Text = "";
                this.Label4.Text = "";
                this.Label5.Text = "票面金额";
                this.Label6.Text = "费用";
                this.Label7.Text = "兑现金额";
                this.Label8.Text = "背书单位";
                this.Label9.Text = "背书金额";
                this.Label10.Text = "利息";
                this.Label11.Text = "录单人";
                this.Label12.Text = "是否公司承担利息";
                this.Label13.Text = "是否公司承担费用";

                //按钮
                this.p_bank.Enabled = false;
                this.p_lv.Enabled = false;
            }

            
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        this.Page.Response.Write("<script>window.returnValue='True';window.close();</script>");
    }
}
